package com.lyn.pluginhost.utils;

import android.annotation.SuppressLint;

import java.lang.reflect.Method;

/**
 * @author longyn 2024/03/13
 * @version 1.0.0
 */
public class PropUtil {

    @SuppressLint("PrivateApi")
    public static String get(String key, String defaultValue) {
        try {
            Class<?> aClass = Class.forName("android.os.SystemProperties");
            Method get = aClass.getDeclaredMethod("get", String.class, String.class);
            get.setAccessible(true);
            return (String) get.invoke(null, key, defaultValue);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    @SuppressLint("PrivateApi")
    public static void set(String key, String value) {
        try {
            Class<?> aClass = Class.forName("android.os.SystemProperties");
            @SuppressLint("DiscouragedPrivateApi") Method set = aClass.getDeclaredMethod("set", String.class, String.class);
            set.setAccessible(true);
            set.invoke(null, key, value);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
